package com.gxh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxh.constant.MessageConstant;
import com.gxh.dto.CategoryDTO;
import com.gxh.dto.CategoryPageQueryDTO;
import com.gxh.entity.Category;
import com.gxh.entity.Machine;
import com.gxh.exception.BaseException;
import com.gxh.exception.DeletionNotAllowedException;
import com.gxh.mapper.CategoryMapper;
import com.gxh.mapper.MachineMapper;
import com.gxh.result.PageResult;
import com.gxh.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private MachineMapper machineMapper;

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        long total = page.getTotal();
        List records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 分类新增
     *
     * @param categoryDTO
     */
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sort",  category.getSort());
        if (categoryMapper.selectCount(queryWrapper) > 0) {
            throw new BaseException("该分类已存在");
        }
        categoryMapper.insertCategory(category);

    }

    /**
     * 分类修改
     *
     * @param categoryDTO
     */
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sort",  category.getSort());
        if (categoryMapper.selectCount(queryWrapper) > 0) {
            throw new BaseException("该分类已存在");
        }
        categoryMapper.updateCategory(category);
    }

    /**
     * 分类批量删除
     * @param ids
     */
    public void deleteBatch(List<Long> ids) {
        //  判断当前分类是否关联了设备
        for (Long id : ids) {
            QueryWrapper<Machine> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category_id", id);
            if (machineMapper.selectCount(queryWrapper) > 0) {
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_MACHINE);
            }
        }
        //执行删除操作
        categoryMapper.deleteBatchIds(ids);
    }

    public Category getById(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return categoryMapper.selectOne(queryWrapper);
    }


    public List<Category> list() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("sort");
        return categoryMapper.selectList(queryWrapper);
    }


}
